﻿using System;
using System.Windows.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Renren.Services;
using Microsoft.Phone.Shell;

namespace Renren
{
    public partial class FriendPickerPage : PhoneApplicationPage
    {
        private const string defaultContent = "搜索";
        private string content;
        private bool searchBoxFocused;
        private DispatcherTimer timer;
        
        public FriendPickerPage()
        {
            content = string.Empty;
            searchBoxFocused = false;
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(0.2);
            timer.Tick += (s, e) =>
                {
                    if (this.SearchBox.Text != string.Empty)
                    {
                        FriendService.SearchFriends(App.ViewModel.FriendList, this.SearchBox.Text);
                    }
                    else
                    {
                        FriendService.GetFriends(App.ViewModel.FriendList);
                    }
                    timer.Stop();
                };

            InitializeComponent();

            this.Loaded += (sender, args) =>
                {
                    this.FriendStreamBox.FriendTap += (s, e) =>
                        {
                            PhoneApplicationService.Current.State["SelectedFriend"] = e.Friend;
                            this.NavigationService.GoBack();
                        };
                    this.SearchBox.GotFocus += (s, e) =>
                        {
                            this.SearchBox.Foreground = new SolidColorBrush(Colors.Black);
                            searchBoxFocused = true;
                            if (this.SearchBox.Text == defaultContent)
                            {
                                this.SearchBox.Text = string.Empty; 
                            }
                        };
                    this.SearchBox.LostFocus += (s, e) =>
                        {
                            this.SearchBox.Foreground = new SolidColorBrush(Colors.Gray);
                            searchBoxFocused = false;
                            if (this.SearchBox.Text == string.Empty)
                            {
                                this.SearchBox.Text = defaultContent; 
                            }
                            timer.Stop();
                        };
                    this.SearchBox.TextChanged += (s, e) =>
                        {
                            if (searchBoxFocused)
                            {
                                if (this.SearchBox.Text != content)
                                {
                                    timer.Start();
                                }
                                content = this.SearchBox.Text; 
                            }
                        };
                };
        }

        //private void Clear_Tap(object sender, GestureEventArgs e)
        //{
        //    this.SearchBox.Text = string.Empty;
        //}
    }
}